package com.wchat.network;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;

import android.os.Environment;
import android.util.Log;

public class Configs {

	public static final int UDP_PORT = 5860;

	public static final int FILE_PORT = 5861;

	public static final int UDP_PACKET_LENGTH = 1024;

	public static final byte[] MESSAGE_HEADER = { 'C', 'H', 'A', 'T' };

	public static final int BROADCAST_WHOAMI_INTERVAL = 1000;

	public static final int EXPIRE_TIME = 3 * BROADCAST_WHOAMI_INTERVAL;

	public static final int CHECK_EXPIRE_INTERVAL = EXPIRE_TIME;

	public static final boolean AUTO_REMOVE_DEVICE_ENABLE = true;

	public static final long generateUniqueDeviceId() {
		return System.currentTimeMillis();
	}

	public static final long generateUniqueGroupId() {
		return System.currentTimeMillis();
	}

	public static final ServerSocket getAvailableServerSocket() {
		ServerSocket serverSocket = null;
		int trys = 0;
		while (serverSocket == null) {
			try {
				serverSocket = new ServerSocket(Configs.FILE_PORT + trys++);
			} catch (IOException e) {

			}
		}
		return serverSocket;
	}

	/* directory */
	private static final String DIR_ROOT = "WCHAT";

	private static final String DIR_IMAGE = "IMAGE";

	private static final String DIR_FILE = "FILE";

	private static final String DIR_SHARE = "SHARE";

	public static final File getRootDir() {
		File file = new File(Environment.getExternalStorageDirectory(), DIR_ROOT);
		if (!file.exists())
			file.mkdirs();
		return file;
	}

	public static final File getImageDir() {
		File file = new File(getRootDir(), DIR_IMAGE);
		if (!file.exists())
			file.mkdirs();
		return file;
	}

	public static final File getFileDir() {
		File file = new File(getRootDir(), DIR_FILE);
		if (!file.exists())
			file.mkdirs();
		return file;
	}

	public static final File getShareDir() {
		File file = new File(getRootDir(), DIR_SHARE);
		if (!file.exists())
			file.mkdirs();
		return file;
	}

	public static boolean isExternalStorageAvailable() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			return true;
		} else {
			return false;
		}
	}

	/* logs */
	public static void assertFail(String message, Exception e) {
		log(message, e);
		// throw new RuntimeException("assert fail", e);
	}

	private static final String TAG = "wchat";

	public static void log(String message) {
		Log.d(TAG, message);
	}

	public static void log(String message, Throwable tr) {
		Log.d(TAG, message, tr);
		/*if (tr != null)
			throw new RuntimeException(tr);*/
	}

	public static void i(String tag, String message) {
		Log.i(tag, message);
	}

	public static void i(String message) {
		i(TAG, message);
	}
}
